<?php
include_once "models/report_model.php";
include_once('models/categories_model.php');
include_once('models/articles_model.php');
include_once('models/alias_model.php');
include_once('controllers/backend_controller.php');
include('components/imageSimple/SimpleImage.php');

class Articles_Controller extends Backend_Controller {
	public $model;
	public $action = 'lists';
	
	public function __construct() {
		parent::__construct();
		// Create Model class
		$this->model = new Articles_Model();
		// Control action
		if(isset($_GET['action'])) $this->action = $_GET['action'];
		$this->{$this->action}();
	}

	public function lists() {
		$artList =  $this->model->getListsArt();
		include 'views/articles/articles-lists.php';
	}

	public function add(){
		$catModel = new Categories_Model();
		$idCat = $catModel->getListCate();

		if(isset($_POST['btn_submit'])) 
		{
			
			if(!empty($_POST['title']) && !empty($_POST['content']) && $this->uploadImg($_FILES) && !empty($_POST['cat'])) 
			{ 
				$img = $_FILES['image']['name'];
				$title = $_POST['title'];
				$createAlias= new Alias_Model();
				$alias= $createAlias->convert2Alias($title);
				$content = $_POST['content'];
				$idcat = $_POST['cat'];
				$this->model->addArt($title,$content,$img,$idcat,$alias);
				$artList =  $this->model->getListsArt();
				include 'views/articles/articles-lists.php';
			}
			else 
			{
				include 'views/articles/addart.php';
			}
		}
		else 
		{
			include 'views/articles/addart.php';
		}
	}
	public function delete(){
		$id = $_GET['id'];
		$img=$this->model->getImage($id);
		$row = mysqli_fetch_array($img);
		$buffer=$row['image'];
	//	exit($buffer);
	
		unlink("media/img/".$buffer);
		
		$this->model->deleteArticles($id);
		$artList = $this->model->getListsArt();
        include'views/articles/articles-lists.php';
	
	
	}
	public function uploadImg($fileupload) {
		$t=time();
        $fileupload["image"]["name"]=$t.'.jpg';
		//exit($_FILES["image"]["name"]);
		//$_FILES["image"]["name"]->resize(250,400);
		$allowedExts = array("gif", "jpeg", "jpg", "png");
		$temp = explode(".", $fileupload["image"]["name"]);
		$extension = end($temp);
		if ((($fileupload["image"]["type"] == "image/gif")
		|| ($fileupload["image"]["type"] == "image/jpeg")
		|| ($fileupload["image"]["type"] == "image/jpg")
		|| ($fileupload["image"]["type"] == "image/pjpeg")
		|| ($fileupload["image"]["type"] == "image/x-png")
		|| ($fileupload["image"]["type"] == "image/png"))
		&& ($fileupload["image"]["size"] < 200000000)
		&& in_array($extension, $allowedExts))
		  {
		  if ($fileupload["image"]["error"] > 0)
		    {
		    echo 'error';
		    return false;
		    }
		    if (file_exists(SITE_URI."/media/img/" . $fileupload["image"]["name"]))
		      {
		      	
		      	return true;
		      }
		    else
		      {	
			 // $random_number = rand(1,10);
			//  $_TARGET=$_FILES["image"]["tmp_name"].$random_number . '.jpg';
			//  var_dump($_FILES["image"]["tmp_name"]); exit();
		        move_uploaded_file($fileupload["image"]["tmp_name"], "media/img/" . $fileupload["image"]["name"]);
				$image = new SimpleImage();
				$image->load("media/img/" . $fileupload["image"]["name"]);
				unlink("media/img/" . $fileupload["image"]["name"]);
				$image->resizeToWidth(250);
				$image->save("media/img/" . $fileupload["image"]["name"]);
		      return true;
		      }
		    }
		else
		  {

		  echo "Invalid file";
		  return false;
		  }
	} 

}

?>